package hashmap01;

import java.util.HashMap;

/*
* HashMap:
* key的特点：HashMap保证key唯一，底层原理依然是通过hashCode()和equals()方法保证key唯一性

* V put(K key, V value)
          将键值对添加到map中
          返回值：
          * 如果当前添加的key与map中已有的key都不同，那么这个put方法返回null
          * 如果当前添加到key与map中已有的key重复，此时会将key对应的新值覆盖掉老值，同时返回老值
* V get(Object key)
          返回指定键所映射的值；如果对于该键来说，此映射不包含任何映射关系，则返回 null。
* int size()
          返回此映射中的键-值映射关系数。
* void clear()
          从此映射中移除所有映射关系。
* */
public class HashMapDemo01 {
    public static void main(String[] args) {
        method01();
        method02();
        method03();
        method04();
    }

    private static void method04() {
        HashMap<Integer, String> hm4 = new HashMap<>();
        hm4.put(42,"abc");
        hm4.put(52,"efg");
        hm4.put(1,"ghk");
        hm4.clear();
        System.out.println(hm4); // {}
    }

    private static void method03() {
        HashMap<Integer, String> hm3 = new HashMap<>();
        hm3.put(42,"abc");
        hm3.put(52,"efg");
        hm3.put(1,"ghk");
        System.out.println(hm3.size()); // 3
    }

    private static void method02() {
        HashMap<Integer, String> hm2 = new HashMap<>();
        hm2.put(42,"abc");
        hm2.put(52,"efg");
        hm2.put(1,"ghk");

        String s = hm2.get(52);
        System.out.println(s); // efg

        String s1 = hm2.get(2);
        System.out.println(s1); // null
    }

    private static void method01() {
        HashMap<Integer, String> hm = new HashMap<>();
        hm.put(42,"abc");
        hm.put(52,"efg");
        hm.put(1,"ghk");
        System.out.println(hm); // {1=ghk, 52=efg, 42=abc}

        String hm1 = hm.put(2, "hij");
        System.out.println(hm1); // null

        String str = hm.put(1, "mn");
        System.out.println(str); //ghk
        System.out.println(hm);//{1=mn, 2=hij, 52=efg, 42=abc}
    }
}
